package com.fanfou.api
{
	import com.abstract.IWeiboAPI;
	import com.events.WeiboEvent;
	import com.fanfou.utils.FanfouConfig;
	import com.fanfou.utils.MethodUtil;
	import com.utils.StringEncoders;
	import com.fanfou.utils.URLUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;

	public class FanfouAPI extends AbstractAPI_FF implements IWeiboAPI
	{
		public function FanfouAPI()
		{
//			config = FanfouConfig.getInstance();
		}
		
//		private var config:FanfouConfig;
		
		private var _type:String;
		
		public function callWeiboAPI(type:String,param:Object):void{
			var url:String;// = MethodUtil.config[type].url;
			switch(type){
				case 'requestToken':
				case 'accessToken':
					url = MethodUtil.config[type].url;
					break;
				default:
					url = config.baseURL + MethodUtil.config[type].url + '.' + config.dataFormat;
					break;
			}
			var method:String = MethodUtil.config[type].method;
			_type = type;
			loadUrl(url,param,method);
		}
		
		override protected function completeHandler(event:Event):void{
			super.completeHandler(event);
			switch(_type){
				case 'requestToken':
				case 'accessToken':
					requestTokenHandler(event);
					break;
				default:
					var evt:WeiboEvent = new WeiboEvent(WeiboEvent.RESULT);
					evt.object = {result:data,type:_type};
					dispatchEvent(evt);
					break;
			}
//			if(_type == 'accesstokenurl'){
//				login(event);
//			}else{
//			}
		}
		
		private function login(event:Event):void{
			var result:String = event.target.data as String;
			if (result.length > 0)
			{
				var urlVar:URLVariables=new URLVariables(event.target.data);
				config.accessTokenKey=urlVar.oauth_token;
				config.accessTokenSecret = urlVar.oauth_token_secret;
				config.username = config.password = '';
			}			
			dispatchEvent(event);
		}
		
		/**
		 * @private
		 */		
		private function requestTokenHandler(event:Event):void
		{
			var needRequestAuthorize:Boolean = config.accessTokenKey.length == 0;
			var result:String = event.currentTarget.data as String;
			if (result.length > 0)
			{
				var urlVar:URLVariables=new URLVariables(event.currentTarget.data);
				config.accessTokenKey=urlVar.oauth_token;
				config.accessTokenSecret=urlVar.oauth_token_secret;
				if (needRequestAuthorize)
				{
					openUserAuthPage();
					needRequestAuthorize=false;
				}
				else
				{
					var evt:WeiboEvent = new WeiboEvent(WeiboEvent.RESULT);
					evt.object = {result:data,type:_type};
					dispatchEvent(evt);
				}
			}
			(event.target as EventDispatcher).removeEventListener(Event.COMPLETE,requestTokenHandler);
		}
		
		/**
		 * 打开用户授权页面
		 * @param pageTarget 窗口模式，默认是_self
		 */
		public function openUserAuthPage(pageTarget:String="_self"):void
		{
			var url:String=config.authorizeURL;
			url+="?oauth_token=" + StringEncoders.urlEncodeUtf8String(config.accessTokenKey);
			url+="&oauth_callback=oob";
			navigateToURL(new URLRequest(url), "_blank");
		}		
	}
}